package com.sz.freemusic.net;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.sz.data.Artist;

public class DataParser {
	
	public static final String ARTIST_ID = "artist_id";
	public static final String ARTIST_NAME = "artist_name";
	public static final String ARTIST_IMG_URL = "artist_image_file";
	
	public List<Artist> parseArtists(String json) throws JSONException {
		JSONObject jsonObject = new JSONObject(json);
		JSONArray artistJSONArray = jsonObject.getJSONArray("dataset");
		ArrayList<Artist> artists = new ArrayList<Artist>();
	
		for (int i = 0; i < artistJSONArray.length(); i++) {
			JSONObject obj = artistJSONArray.getJSONObject(i);
			if (obj != null) {
				Artist artist = new Artist(obj.getString(ARTIST_ID), obj.getString(ARTIST_NAME));
				artist.setImageUrl(obj.getString(ARTIST_IMG_URL));
				artists.add(artist);
			;
				
			}
			
		}
		
		return artists;
	}
	
}
